/**
 * Created by haoran.shu on 2018/11/16 10:59.
 */
const router = new require('koa-router')();
const logger = require('log4js').getLogger('tactics');
const mongo = require('../dbs/mongodb');

// 新建策略信息 name,code
router.post('/save', async (ctx) => {
  let params = ctx.request.body;
  logger.info('save tactics name: ', params.name);
  let n = await mongo.insert('tactics', {
    tel: ctx.session.tel,
    name: params.name,
    code: '',
    time: 1,
    product: 'GOLD',
    create_time: Date.now(), // 策略上传时间
    update_time: Date.now() // 策略最近修改时间
  });
  ctx.body = { code: 1,_id:n.insertedId  };
});

// 删除策略信息, _id
router.post('/delete', async (ctx) => {
  let params = ctx.request.body;
  logger.info('delete tactics params: ', params);
  mongo.deleteById('tactics', params._id);
  ctx.body = { code: 1 };
});

// 获取策略信息, _id
router.get('/info', async (ctx) => {
  let params = ctx.query;
  logger.info('info tactics params: ', params);
  let t = await mongo.findById('tactics', params._id, {
    name: 1, time: 1, code: 1, product: 1
  });
  ctx.body = { code: 1, tactic: t };
});

// 修改策略信息, _id
router.post('/update', async (ctx) => {
  let params = ctx.request.body;
  logger.info('update tactics params: ', params);
  mongo.upsetById('tactics', params._id, {
    name: params.name,
    code: params.code,
    product: params.product,
    time: Number(params.time),
    update_time: Date.now() // 策略最近修改时间
  });
  ctx.body = { code: 1 };
});



module.exports = router;
